package cn.codergege.spiderjd.spider

import cn.codergege.spiderjd.entity.Item
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.openqa.selenium.JavascriptExecutor
import org.openqa.selenium.WebDriver
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.chrome.ChromeOptions
import org.openqa.selenium.support.ui.WebDriverWait
import org.springframework.stereotype.Component

@Component
class AllSortSpider(val url: String = "https://www.jd.com/allSort.aspx") {
    lateinit var driver: WebDriver

    init {
        //System.setProperty("webdriver.chrome.driver", "D:\\Toolkits\\Curl\\curl-7.62.0-win64-mingw\\bin\\chromedriver.exe");
    }

    fun boot(url: String = this.url) {
        driver = ChromeDriver(ChromeOptions().addArguments("headless"))
//        driver = ChromeDriver()
        // 所有商品类别入口页
        driver.get(url)
        // 等待页面加载完毕
        waitUntilReady(driver)

        // 获取页面源码, 给 Jsoup 解析
        val pageSource = driver.pageSource
        val doc = Jsoup.parse(pageSource)

        // 获取所有 item
        val items = parsePageAllsort(doc)
        // 下面是演示验证
        items.filter {
            it.category.contains("电子书刊")
        }.filter {
            it.mid.contains("科技")
        }.forEach(::println)
        println(items.size)

        // 关闭退出 driver
        driver.close()
        driver.quit()
    }

    // 解析页面源码, 返回 items
    private fun parsePageAllsort(doc: Document): ArrayList<Item> {
        val items = ArrayList<Item>()
        doc.select("div.category-items div[data-idx]")
            .forEach {
                val dataIdx = it.attr("data-idx")
                // 大类名称
                val categoryName = it.select("div.mt span").text()
                it.select("div.mc div.items > dl")
                    .forEach {
                        // 中类名称
                        val midItemA = it.select("dt > a").first()
//                        val midItem = MidItem(midItemA.text(), midItemA.attr("href"))
                        // 小类
                        items.addAll(it.select("dd > a").map {
                            Item(it.text(),
                                it.attr("href"),
                                midItemA.text(),
                                midItemA.attr("href"),
                                categoryName)
                        })
                    }
            }
        return items
    }

    /**
     *  等待页面装载完毕
     */
    fun waitUntilReady(driver: WebDriver) {
        val executor = driver as JavascriptExecutor
        WebDriverWait(driver, 10)
            .until { executor.executeScript("return document.readyState") == "complete" }
    }
}